<%@ page contentType="text/html; charset=utf-8" language="java" extends="org.zizi.service.JspPageServlet" %>
<%@page import="java.util.Date"%>
<%@page import="org.zizi.util.DateUtil"%>
<%@page import="org.zizi.manager.LoginManager"%>
<%@page import="org.zizi.modal.User"%>
<%@page import="org.zizi.manager.impl.DaoManager"%>
<%@page import="org.zizi.manager.Managers"%>
<%@page import="org.zizi.dao.VatTypeDao"%>
<%@page import="org.zizi.modal.VatType"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="org.zizi.modal.InlandRevenueOrg"%>
<%@page import="org.zizi.dao.UserInfoDao"%>
<%@page import="org.zizi.modal.UserInfo"%>
<%@page import="org.zizi.dao.InlandRevenueOrgDao"%>
<%@page import="org.zizi.dao.VatCreditDao"%>
<%@page import="org.zizi.modal.VatCredit"%>
<%@page import="org.zizi.dao.UserDao"%>
<%@page import="org.zizi.modal.VatCreditInvoice"%>

<%
	String szId = request.getParameter("id");
	long id = szId == null ? 0 : Long.parseLong(szId);

	VatCreditDao vcDao = Managers.get(DaoManager.class).getDao(VatCreditDao.class);
	VatCredit credit = vcDao.getVatCredit(id);
	List<VatCreditInvoice> invoices = vcDao.getVatCreditInvoices(id);
	
	UserInfoDao uiDao = Managers.get(DaoManager.class).getDao(UserInfoDao.class);
	UserInfo ui = null;
	if (credit != null) ui = uiDao.getUserInfo(credit.getIdentify());
	
	VatTypeDao tDao = Managers.get(DaoManager.class).getDao(VatTypeDao.class);
	UserDao uDao = Managers.get(DaoManager.class).getDao(UserDao.class);
	User creator = null;			//提交人
	User lastModify = null;		//最后修改人
	if (credit != null)
	{
		if (credit.getCreatorId() != null) creator = uDao.getUser(credit.getCreatorId());
		if (credit.getLastModifyId() != null) lastModify = uDao.getUser(credit.getLastModifyId());
	}
	
	List<VatCreditInvoice> sepInvoices = new ArrayList<VatCreditInvoice>();
	List<VatCreditInvoice> manInvoices = new ArrayList<VatCreditInvoice>();
	List<VatCreditInvoice> fixInvoices = new ArrayList<VatCreditInvoice>();
	List<VatCreditInvoice> othInvoices = new ArrayList<VatCreditInvoice>();
	for (VatCreditInvoice inv : invoices)
	{
		inv.setType(tDao.getVatType(inv.getInvoiceTypeId()));
		
		if (VatCreditInvoice.SEP.equals(inv.getInvoiceCategory()))
			sepInvoices.add(inv);
		if (VatCreditInvoice.MAN.equals(inv.getInvoiceCategory()))
			manInvoices.add(inv);
		if (VatCreditInvoice.FIX.equals(inv.getInvoiceCategory()))
			fixInvoices.add(inv);
		if (VatCreditInvoice.OTH.equals(inv.getInvoiceCategory()))
			othInvoices.add(inv);
	}
	
	InlandRevenueOrgDao iroDao = Managers.get(DaoManager.class).getDao(InlandRevenueOrgDao.class);
	InlandRevenueOrg org = null;
	if (ui != null) org = iroDao.getInlandRevenueOrg(ui.getOrgCode());
	
	Double preLeftCredit = null;
	if (ui != null && credit != null) preLeftCredit = vcDao.getPreviousLeftCredit(ui.getIdentify(), credit.getYear(), credit.getMonth());
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>专用设置和技术维护费抵减系统</title>
<style type="text/css">
body {margin:5px; padding:0px; font-size:12px; background-color:#FCF0E9; text-align:center;}
.view_table {margin-top:15px; border-collapse:collapse; width:800px; font-size:12px;}
.view_table td {border:solid 1px #CCC;}
.view_table td.title {background-color:#DDD; font-weight:bold;}
</style>
</head>
<body>
	<table class="view_table" align="center" cellspacing="0" cellpadding="5" border="0">
		<tr>
			<td colspan="7" class="title">日期信息</td>
		</tr>
		<tr>
			<td colspan="2">填开日期：<%=credit == null ? "" : DateUtil.format(credit.getSubmitTime()) %></td>
			<td colspan="3">所属期：<%=credit == null ? "" : (credit.getYear() + "-" + credit.getMonth()) %></td>
			<td colspan="2">金额单位：元</td>
		</tr>
		<tr>
			<td colspan="2">提交人：<%=creator == null ? "接口提交" : creator.getName() %></td>
			<td colspan="3">最后修改日期：<%=credit == null ? "" : DateUtil.format(credit.getLastModifyTime()) %></td>
			<td colspan="2">最后修改人：<%=lastModify == null ? "接口提交" : lastModify.getName() %></td>
		</tr>
		<tr>
			<td colspan="7" class="title">纳税人信息</td>
		</tr>
		<tr>
			<td colspan="2">纳税人识别号</td>
			<td colspan="2"><%=credit == null ? "" : credit.getIdentify() %></td>
			<td colspan="1">纳税人名称</td>
			<td colspan="2"><%=ui == null ? "" : ui.getName() %></td>
		</tr>
		<tr>
			<td colspan="2">纳税人地址</td>
			<td colspan="2"><%=ui == null ? "" : ui.getAddress() %></td>
			<td colspan="1">主管国家机关</td>
			<td colspan="2"><%=org == null ? "" : org.getName() %></td>
		</tr>
		<tr>
			<td colspan="7" class="title">增值税应纳税额抵减凭证信息</td>
		</tr>
		<tr>
			<td colspan="2">抵减凭证类别</td>
			<td>发票代码</td>
			<td>发票号码</td>
			<td>项目内容</td>
			<td>价税合计金额</td>
			<td>日期</td>
		</tr>
		<%
			///////专用发票//////////////////////
			
			out.println("<tr>");
			out.println("<td colspan='2' rowspan='" + (sepInvoices.isEmpty() ? 1 : sepInvoices.size()) + "'>增值税专用发票</td>");
			if (sepInvoices.isEmpty())
			{
				out.println("<td>&nbsp;</td>");
				out.println("<td>&nbsp;</td>");
				out.println("<td>&nbsp;</td>");
				out.println("<td>&nbsp;</td>");
				out.println("<td>&nbsp;</td>");
			}
			else
			{
				VatCreditInvoice inv = sepInvoices.get(0);
				out.println("<td>" + inv.getInvoiceCode() + "</td>");
				out.println("<td>" + inv.getInvoiceNum() + "</td>");
				out.println("<td>" + inv.getType().getName() + "</td>");
				out.println("<td>" + inv.getInvoiceMoney() + "</td>");
				out.println("<td>" + DateUtil.format(inv.getInvoiceDate()) + "</td>");
			}
			out.println("</tr>");
			for (int i = 1; i < sepInvoices.size(); i++)
			{
				VatCreditInvoice inv = sepInvoices.get(i);
				out.println("<tr>");
				out.println("<td>" + inv.getInvoiceCode() + "</td>");
				out.println("<td>" + inv.getInvoiceNum() + "</td>");
				out.println("<td>" + inv.getType().getName() + "</td>");
				out.println("<td>" + inv.getInvoiceMoney() + "</td>");
				out.println("<td>" + DateUtil.format(inv.getInvoiceDate()) + "</td>");
				out.println("</tr>");
			}
			
			////////通用手工发票/////////////////
			
			out.println("<tr>");
			out.println("<td rowspan='" + (manInvoices.size() + fixInvoices.size() < 2 ? 2 : manInvoices.size() + fixInvoices.size()) + "'>普通发票</td>");
			out.println("<td rowspan='" + (manInvoices.isEmpty() ? 1 : manInvoices.size()) + "'>增值税专用发票</td>");
			if (manInvoices.isEmpty())
			{
				out.println("<td>&nbsp;</td>");
				out.println("<td>&nbsp;</td>");
				out.println("<td>&nbsp;</td>");
				out.println("<td>&nbsp;</td>");
				out.println("<td>&nbsp;</td>");
			}
			else
			{
				VatCreditInvoice inv = manInvoices.get(0);
				out.println("<td>" + inv.getInvoiceCode() + "</td>");
				out.println("<td>" + inv.getInvoiceNum() + "</td>");
				out.println("<td>" + inv.getType().getName() + "</td>");
				out.println("<td>" + inv.getInvoiceMoney() + "</td>");
				out.println("<td>" + DateUtil.format(inv.getInvoiceDate()) + "</td>");
			}
			out.println("</tr>");
			for (int i = 1; i < manInvoices.size(); i++)
			{
				VatCreditInvoice inv = manInvoices.get(i);
				out.println("<tr>");
				out.println("<td>" + inv.getInvoiceCode() + "</td>");
				out.println("<td>" + inv.getInvoiceNum() + "</td>");
				out.println("<td>" + inv.getType().getName() + "</td>");
				out.println("<td>" + inv.getInvoiceMoney() + "</td>");
				out.println("<td>" + DateUtil.format(inv.getInvoiceDate()) + "</td>");
				out.println("</tr>");
			}
			
			////////定额发票/////////////////
			
			out.println("<tr>");
			out.println("<td rowspan='" + (fixInvoices.isEmpty() ? 1 : fixInvoices.size()) + "'>增值税专用发票</td>");
			if (fixInvoices.isEmpty())
			{
				out.println("<td>&nbsp;</td>");
				out.println("<td>&nbsp;</td>");
				out.println("<td>&nbsp;</td>");
				out.println("<td>&nbsp;</td>");
				out.println("<td>&nbsp;</td>");
			}
			else
			{
				VatCreditInvoice inv = fixInvoices.get(0);
				out.println("<td>" + inv.getInvoiceCode() + "</td>");
				out.println("<td>" + inv.getInvoiceNum() + "</td>");
				out.println("<td>" + inv.getType().getName() + "</td>");
				out.println("<td>" + inv.getInvoiceMoney() + "</td>");
				out.println("<td>" + DateUtil.format(inv.getInvoiceDate()) + "</td>");
			}
			out.println("</tr>");
			for (int i = 1; i < fixInvoices.size(); i++)
			{
				VatCreditInvoice inv = fixInvoices.get(i);
				out.println("<tr>");
				out.println("<td>" + inv.getInvoiceCode() + "</td>");
				out.println("<td>" + inv.getInvoiceNum() + "</td>");
				out.println("<td>" + inv.getType().getName() + "</td>");
				out.println("<td>" + inv.getInvoiceMoney() + "</td>");
				out.println("<td>" + DateUtil.format(inv.getInvoiceDate()) + "</td>");
				out.println("</tr>");
			}
			
			////////其它发票/////////////////
			
			out.println("<tr>");
			out.println("<td colspan='2' rowspan='" + (othInvoices.isEmpty() ? 1 : othInvoices.size()) + "'>其它</td>");
			if (othInvoices.isEmpty())
			{
				out.println("<td>&nbsp;</td>");
				out.println("<td>&nbsp;</td>");
				out.println("<td>&nbsp;</td>");
				out.println("<td>&nbsp;</td>");
				out.println("<td>&nbsp;</td>");
			}
			else
			{
				VatCreditInvoice inv = othInvoices.get(0);
				out.println("<td>-</td>");
				out.println("<td>-</td>");
				out.println("<td>" + inv.getType().getName() + "</td>");
				out.println("<td>" + inv.getInvoiceMoney() + "</td>");
				out.println("<td>" + DateUtil.format(inv.getInvoiceDate()) + "</td>");
			}
			out.println("</tr>");
			for (int i = 1; i < othInvoices.size(); i++)
			{
				VatCreditInvoice inv = othInvoices.get(i);
				out.println("<tr>");
				out.println("<td>-</td>");
				out.println("<td>-</td>");
				out.println("<td>" + inv.getType().getName() + "</td>");
				out.println("<td>" + inv.getInvoiceMoney() + "</td>");
				out.println("<td>" + DateUtil.format(inv.getInvoiceDate()) + "</td>");
				out.println("</tr>");
			}
		%>
		<tr>
			<td colspan="7" class="title">增值税应纳税额抵减税额信息<%= credit.isValid() ? "" : "<font color='red'>（作废清单可能会存在金额数据错误）</font>" %></td>
		</tr>
		<tr>
			<td colspan="3">可抵减税额</td>
			<td rowspan="2" colspan="2">本期申报抵减税额</td>
			<td rowspan="2" colspan="2">本期未抵减税额</td>
		</tr>
		<tr>
			<td>本期应抵减税额</td>
			<td>上期未抵减税额</td>
			<td>小计</td>
		</tr>
		<tr>
			<td><%=credit == null || preLeftCredit == null ? "<无>" : Math.floor((credit.getApplyVatCredit() + credit.getThisLeftCredit() - preLeftCredit) * 100)/(double)100 %></td>
			<td><%=preLeftCredit == null ? "<无>" : preLeftCredit %></td>
			<td><%=credit == null ? "<无>" : credit.getApplyVatCredit() + credit.getThisLeftCredit() %></td>
			<td colspan="2"><%=credit == null ? "<无>" : credit.getApplyVatCredit() %></td>
			<td colspan="2"><%=credit == null ? "<无>" : credit.getThisLeftCredit() %></td>
		</tr>
		<tr>
			<td colspan="7" align="left" style="line-height:20px; border:0; font-size:12px;">
				<b>填表说明：</b><br/>
				1. 日期格式请写成：2012-01-01。<br/>
				2. 项目内容栏：抵减凭证为增值税专用发票的，项目内容栏填写增值税税控系统专用设备类别；抵减凭证为普通发票的，项目内容栏填写费用类别。 <br/>
				3. 日期栏：抵减凭证为增值税专用地票的，日期栏填写增值税税控系统初始发行日期；抵减凭证为普通发票的，日期栏填写缴纳技术维护费起止时间。 <br/>
				4. 本期未抵减税额 = 可抵减金额 - 本期申报抵减税额。 <br/>
				5. 本期应抵减税额栏：填写本清单中增值税专用发票和普通发票价税合计金额栏的合计金额。 <br/>
			</td>
		</tr>
	</table>
</body>
</html>